# -*- coding: utf-8 -*-
# File : gen_mock_data.py
# Author: taoyahui
# Date : 2021/10/19
from services.food_service import query_food
from services.sell_service import query_order, create_order
from models.create_food import CreateFood
from models.create_order import CreateOrder
from config import contract_address, contract_abi, admin_address
from utils import id_utils, common_utils
import _thread
import time
import random


def query_random_food():
    trans_list = CreateFood.query.all()
    trans_list = [item.to_json() for item in trans_list]
    num = random.randint(0, len(trans_list)-1)
    food = trans_list[num]
    print(food)
    return food['food_id']


def query_random_order():
    trans_list = CreateOrder.query.all()
    trans_list = [item.to_json() for item in trans_list]
    num = random.randint(0, len(trans_list)-1)
    order = trans_list[num]
    print(order)
    return order


def gen_mock_order():
    while True:
        print("^" * 20 + "开始产生模拟订单" + "^" * 20)
        print(time.ctime(time.time()))
        num = random.randint(2000,10000)
        food_id = query_random_food()
        price = random.randint(5,10)
        order_no = next(id_utils.unique_sequence)
        res = common_utils.common_bc_req(admin_address, 'FoodTraceNew', 'createOrder', [order_no, num, food_id, price],
                                         contract_address,
                                         contract_abi)
        rand_sleep = random.randint(90, 120)
        time.sleep(rand_sleep)


def gen_mock_produce():
    while True:
        print("^" * 20 + "开始产生模拟《生产》订单" + "^" * 20)
        print(time.ctime(time.time()))
        order = query_random_order()
        order_no = order['order_no']
        order_type = random.randint(0,1)
        food_id = order['food_id']
        price = random.randint(2,order['price'])
        num = random.randint(100,500)
        sub_no = next(id_utils.unique_sequence)
        res = common_utils.common_bc_req(admin_address, 'FoodTraceNew', 'Produces',
                                         [order_no, sub_no, order_type, food_id, price, num], contract_address,
                                         contract_abi)
        rand_sleep = random.randint(30, 60)
        time.sleep(rand_sleep)


def gen_mock_buy():
    while True:
        print("^" * 20 + "开始产生模拟《收购》订单" + "^" * 20)
        print(time.ctime(time.time()))
        order = query_random_order()
        order_no = order['order_no']
        order_type = random.randint(0,1)
        food_id = order['food_id']
        price = random.randint(2,order['price'])
        num = random.randint(100,1000)
        sub_no = next(id_utils.unique_sequence)
        res = common_utils.common_bc_req(admin_address, 'FoodTraceNew', 'Buy',
                                         [order_no, sub_no, order_type, food_id, price, num], contract_address,
                                         contract_abi)
        rand_sleep = random.randint(40, 70)
        time.sleep(rand_sleep)


def gen_mock_ship():
    while True:
        print("^" * 20 + "开始产生模拟《运输》订单" + "^" * 20)
        print(time.ctime(time.time()))
        order = query_random_order()
        order_no = order['order_no']
        order_type = random.randint(0,1)
        num = random.randint(100,1000)
        from_place = '镇江'
        to_place = '常州'
        sub_no = next(id_utils.unique_sequence)
        res = common_utils.common_bc_req(admin_address, 'FoodTraceNew', 'Transport',
                                         [order_no, sub_no, order_type, num, from_place, to_place], contract_address,
                                         contract_abi)
        rand_sleep = random.randint(60, 90)
        time.sleep(rand_sleep)

def print_time(threadName, delay):
    count = 0
    while count < 5:
        time.sleep(delay)
        count += 1
        print("%s: %s" % (threadName, time.ctime(time.time())))


# Create two threads as follows
try:
    _thread.start_new_thread(gen_mock_order)
    _thread.start_new_thread(gen_mock_produce)
    _thread.start_new_thread(gen_mock_buy)
    _thread.start_new_thread(gen_mock_ship)
    # _thread.start_new_thread(print_time, ("Thread-2", 4,))
except:
    print("Error: unable to start thread")

while 1:
    pass


